package com.example.lib_login.pwd

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.example.lib_login.LoginActivity
import com.example.lib_login.LoginViewModel
import com.example.lib_login.R
import com.example.lib_login.code.VerificationCodeFragment
import com.example.lib_login.dataBinding.Events
import com.example.lib_login.databinding.FragmentPasswordBinding
import com.example.lib_login.view.InteractionManagement


class PasswordFragment : Fragment() {
    private lateinit var binding:FragmentPasswordBinding
    private val viewModel:LoginViewModel by activityViewModels()
    private val event = Events()
    private lateinit var callback: InteractionManagement

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentPasswordBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        callback = object :InteractionManagement{
            override fun start() {
                requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

            override fun stop() {
                requireActivity().window.setFlags(
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }
        }

        binding.binding = this.binding
        binding.events = event
        binding.viewModel = viewModel
        binding.callback = callback
        binding.activity = requireActivity() as LoginActivity?



        binding.pwdTextView.addTextChangedListener(
            onTextChanged = {text, start, before, count ->
                if(text == null) return@addTextChangedListener
                if(text.isNotEmpty()){
                    binding.button.isEnabled = true
                    binding.clearBtn.visibility = View.VISIBLE
                }else{
                    binding.button.isEnabled = false
                    binding.clearBtn.visibility = View.INVISIBLE
                }
            }
        )

        binding.back.setOnClickListener {
            requireActivity().supportFragmentManager.beginTransaction().apply {
                requireActivity().supportFragmentManager.popBackStack()
                commit()
            }
        }

        binding.clearBtn.setOnClickListener {
            binding.pwdTextView.text.clear()
        }



    }



}